package com.nine.mymall.product.service.impl;

import com.nine.mymall.product.dao.AttrAttrgroupRelationDao;
import com.nine.mymall.product.entity.AttrEntity;
import com.nine.mymall.product.service.AttrAttrgroupRelationService;
import com.nine.mymall.product.service.AttrService;
import com.nine.mymall.product.vo.AttrGroupRelationVo;
import com.nine.mymall.product.vo.AttrGroupWithAttrsVo;
import com.nine.mymall.product.vo.SkuItemVo;
import com.nine.mymall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nine.common.utils.PageUtils;
import com.nine.common.utils.Query;

import com.nine.mymall.product.dao.AttrGroupDao;
import com.nine.mymall.product.entity.AttrGroupEntity;
import com.nine.mymall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // 0：查询所有
        if (catelogId.longValue() == 0) {
            // 不需要查询 catelog_id，默认就是所有的
        } else {
            // select * from pms_attr_group where catelog_id = ? and (attr_group_id = key or attr_group_name = key)
            wrapper.eq("catelog_id", catelogId);
        }

        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    @Override
    public void deleteRelations(AttrGroupRelationVo[] relationVos) {

        List<AttrGroupRelationVo> attrGroupRelationVos = Arrays.asList(relationVos);

        List<AttrGroupRelationVo> agrvList = attrGroupRelationVos.stream().map((item) -> {
            AttrGroupRelationVo attrGroupRelationVo = new AttrGroupRelationVo();
            BeanUtils.copyProperties(item, attrGroupRelationVo);
            return attrGroupRelationVo;
        }).collect(Collectors.toList());

        relationDao.deleteBatchRelations(agrvList);
    }

    /**
     * 根据 分类 id 查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        // 1、查询分组信息
        QueryWrapper<AttrGroupEntity> groupEntityQueryWrapper = new QueryWrapper<>();
        groupEntityQueryWrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroupEntities = this.list(groupEntityQueryWrapper);

        // 2、查询分组下的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            // vo数据复制
            BeanUtils.copyProperties(group, attrGroupWithAttrsVo);
            // 查询分组下属性，并赋值
            List<AttrEntity> attrEntities = attrService.getRelationAttr(group.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return vos;
    }

    /**
     * 查出当前spu对应的所有属性的分组信息，以及当前分组下所有属性对应的值
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catelogId) {
        return attrGroupDao.getAttrGroupWithAttrsBySpuId(spuId, catelogId);
    }

}